var baseHttpUrl = "http://192.168.0.103:8081/";
var LoginUrl = baseHttpUrl + "api/login"; //登陆接口
var Rettoken = baseHttpUrl + "api/resetToken"; //重新获取token
var ConfigInit = baseHttpUrl + "api/config/init"; //配置信息
var GetMemberInfo = baseHttpUrl + "api/member/info"; //获取会员信息
var ModifyUserInfo = baseHttpUrl + "api/member/update"; //修改用户信息
var ModifyAlipay = baseHttpUrl + "api/member/setPay"; //修改支付信息
var SendBindCode = baseHttpUrl + "api/send/bind"; //发送绑定手机验证码
var OrderCount = baseHttpUrl + "api/team/myCount"; //个人统计信息
var TodayCount = baseHttpUrl + "api/team/todayCount"; //当日团队统计
var WeekCount = baseHttpUrl + "api/team/weekCount"; //本周团队统计
var MonthCount = baseHttpUrl + "api/team/monthCount"; //本月团队统计
var ThreeMonthCount = baseHttpUrl + "api/team/threeMonthCount"; //最近三月团队统计
var CustomCount = baseHttpUrl + "api/team/customCount"; //自定义日期团队统计
var GetNotice = baseHttpUrl + "api/service/notice"; //获取公告
var BalanceDetail = baseHttpUrl + "api/accounting/list"; //金币明细
var PacketList = baseHttpUrl + "api/packet/list"; //红包列表
var RoomPacketList = baseHttpUrl + "api/packet/roomPacketList"; //房间初始化红包列表
var PacketDetail = baseHttpUrl + "api/packet/listGrab"; //红包详情
var CheckUpdate = baseHttpUrl + "api/config/checkUpdate"; //检查更新
var UploadAlipay = baseHttpUrl + "api/member/upload/alipay"; //上传支付宝收款码
var UploadWechat = baseHttpUrl + "api/member/upload/wechat"; //上传微信收款码
var DepositList = baseHttpUrl + "api/topup/listDeposit"; //充值列表
var WithdrawList = baseHttpUrl + "api/topup/listWithdraw"; //提现列表
var CenterList = baseHttpUrl + "api/game/centers"; //大厅列表
var RoomList = baseHttpUrl + "api/game/rooms"; //房间列表
var GotoRoom = baseHttpUrl + "api/game/gotoRoom"; //进入房间
var GotoRoomVip = baseHttpUrl + "api/game/gotoRoomVip"; //进入VIP房间
var PacketGrab = baseHttpUrl + "api/packet/grab"; //抢包
var PacketSend = baseHttpUrl + "api/packet/send"; //发包
var ExitRoom = baseHttpUrl + "api/game/exitRoom"; //退出房间
var CreateRoom = baseHttpUrl + "api/game/createRoom"; //创建房间
var MyRoomList = baseHttpUrl + "api/game/myRooms"; //我的房间列表
var GotoMyRoom = baseHttpUrl + "api/game/gotoMyRoom"; //进入我创建的房间
var BindMobile = baseHttpUrl + "api/bindMobile"; //绑定手机

//mui的toast
function mymui(msg) {
	mui.toast(msg, {
		duration: 'short',
		type: 'richtext' //解决有的手机前面显示应用名称的问题
	})
}

//显示原生的toast
function myNativeToast(msg) {
	plus.nativeUI.toast(msg);
}

var mToken = "";

function getTooken() {
	if(plus.storage.getItem('tooken') !== '') {
		return plus.storage.getItem('tooken');
	}
	return "";
}

var mConsole = {};
mConsole.log = function(msg) {
	var bool = true;
	if(bool) {
		console.log(msg);
	}
}

function copyArr(arr) {
	var res = [];
	for(var i = 0; i < arr.length; i++) {
		res.push(arr[i]);
	};
	return res;
}

mui.postJSON = function(url, data, callback) {
	mui.ajax({
		url: url,
		type: 'POST',
		timeout: 30000,
		data: data ? data : null,
		dataType: "json",
		headers: {
			'Content-Type': 'application/json',
			'charset': 'utf-8',
			'token': plus.storage.getItem("tooken")
		},
		success: function(result) {
			if(callback && typeof callback === "function") {
				callback(result);
			}
			if(result.code == 999) {
				if(!plus.webview.currentWebview().getURL().endWith("index.html")) {
					var index = plus.webview.getLaunchWebview();
					index.show();
					var wvs = plus.webview.all();
					for(var i = 0; i < wvs.length; i++) {
						if(wvs[i].id == index.id) {
							continue;
						}
						wvs[i].hide();
						wvs[i].close();
					}
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			mymui("网络异常：" + type);
		}
	});
}

mui.postParam = function(url, data, callback) {
	mui.ajax({
		url: url,
		type: 'POST',
		timeout: 30000,
		data: data ? data : null,
		dataType: "json",
		headers: {
			'charset': 'utf-8',
			'token': plus.storage.getItem("tooken")
		},
		success: function(result) {
			if(callback && typeof callback === "function") {
				callback(result);
			}
			if(result.code == 999) {
				if(!plus.webview.currentWebview().getURL().endWith("index.html")) {
					var index = plus.webview.getLaunchWebview();
					index.show();
					var wvs = plus.webview.all();
					for(var i = 0; i < wvs.length; i++) {
						if(wvs[i].id == index.id) {
							continue;
						}
						wvs[i].hide();
						wvs[i].close();
					}
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			mymui("网络异常：" + type);
		}
	});
}

var util = function() {
	var toStr = function(str) {
		if(str) {
			return String(str);
		} else {
			console.error(str + "不是String类型");
			return "";
		}
	};

	return {
		toStr: toStr
	};
}();

//右滑关闭 需要放入mui.plusReady里
function swipeBack() {
	//获取当前窗口,下面代码按界面任意地方都可右滑关闭
	var ws = plus.webview.currentWebview();
	//监听当前侧滑窗口的右滑
	ws.drag({
		direction: 'right',
		moveMode: 'followFinger'
	}, {
		view: plus.runtime.appid,
		moveMode: 'silent'
	}, function(e) {
		//滑动到end执行mui.back()事件
		if(e.type == 'end' && e.result) {
			mui.back();
		}
	});
}

String.prototype.startWith = function(str) {
	var reg = new RegExp("^" + str);
	return reg.test(this);
}

String.prototype.endWith = function(str) {
	var reg = new RegExp(str + "$");
	return reg.test(this);
}